<template>
  <div class="container">
    <h1>二级及以上医疗机构医疗救治工作有关情况日报表</h1>
    <div class="form-header">
      <div>
        <label>填表人：</label>
        <input type="text" v-model="formData.basicInfo.reporter" placeholder="请输入填表人姓名">
      </div>
      <div>
        <label>联系方式：</label>
        <input type="text" v-model="formData.basicInfo.contact" placeholder="请输入联系方式">
      </div>
      <div>
        <label>填表日期：</label>
        <input type="date" v-model="formData.basicInfo.reportDate">
      </div>
    </div>

    <table>
      <tr class="header-row">
        <th width="60" colspan="4">填报类别</th>
        <th colspan="1">数量</th>
      </tr>
      <template v-for="row in tableData[0].rows" :key="row.id">
        <!-- 发热门诊、普通门诊、急诊 -->
        <tr v-if="[1,3,5].includes(row.id)">
          <td :rowspan="2">{{ row.indicator }}</td>
          <td class="note" colspan="3">{{ row.note }}</td>
          <td><input type="number" v-model="row.count" @keydown.enter="handleEnter($event)"></td>
        </tr>
        <tr v-else-if="[2,4,6].includes(row.id)">
          <td class="note" colspan="3">{{ row.note }}</td>
          <td><input type="number" v-model="row.count" @keydown.enter="handleEnter($event)"></td>
        </tr>
        <!-- 床位数及在院患者人数 -->
        <tr v-else-if="row.id === 7">
          <td :rowspan="21">{{ row.indicator }}</td>
          <td class="note" :rowspan="4">{{ row.note }}</td>
          <td class="note" colspan="2">{{ row.detail }}</td>
          <td><input type="number" v-model="row.count" @keydown.enter="handleEnter($event)"></td>
        </tr>
        <!-- 其他行的处理... -->
      </template>
    </table>

    <div class="button-container">
      <button class="save-btn" @click="saveData">保存数据</button>
      <button class="export-btn" @click="exportExcel">导出汇总表</button>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'EvaluateForm',
  data() {
    return {
      tableData: [
        {
          rows: [
            {
                id: 1,
                indicator: "发热门诊(人次)",
                category: "发热门诊",
                sub_category: "每日总诊疗量",
                count: "",
                note: "每日总诊疗量"
            },
            {
                id: 2,
                indicator: "发热门诊(人次)",
                category: "发热门诊",
                sub_category: "新冠病毒核酸或抗原阳性数量",
                count: "",
                note: "新冠病毒核酸或抗原阳性数量"
            },
            {
                id: 3,
                indicator: "普通门诊(人次)",
                category: "普通门诊",
                sub_category: "每日总诊疗量",
                count: "",
                note: "每日总诊疗量"
            },
            {
                id: 4,
                indicator: "普通门诊(人次)",
                category: "普通门诊",
                sub_category: "新冠病毒核酸或抗原阳性数量",
                count: "",
                note: "新冠病毒核酸或抗原阳性数量"
            },
            {
                id: 5,
                indicator: "急诊(人次)",
                category: "急诊",
                sub_category: "每日总诊疗量",
                count: "",
                note: "每日总诊疗量"
            },
            {
                id: 6,
                indicator: "急诊(人次)",
                category: "急诊",
                sub_category: "新冠病毒核酸或抗原阳性数量",
                count: "",
                note: "新冠病毒核酸或抗原阳性数量"
            },
            {
                id: 7,
                indicator: "床位数及在院患者人数(人)",
                category: "总数",
                sub_category: "患者",
                count: "",
                note: "总数",
                detail: "患者"
            },
            {
                id: 8,
                indicator: "床位数及在院患者人数(人)",
                category: "总数",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "总数",
                detail: "新冠病毒核酸或抗原阳性数"
            },
            {
                id: 9,
                indicator: "床位数及在院患者人数(人)",
                category: "总数",
                sub_category: "当日入院人次数",
                count: "",
                note: "总数",
                detail: "当日入院人次数"
            },
            {
                id: 10,
                indicator: "床位数及在院患者人数(人)",
                category: "总数",
                sub_category: "当日出院人次数",
                count: "",
                note: "总数",
                detail: "当日出院人次数"
            },
            {
                id: 11,
                indicator: "床位数及在院患者人数(人)",
                category: "综合ICU",
                sub_category: "患者",
                count: "",
                note: "总数",
                detail: "患者"
            },
            {
                id: 12,
                indicator: "床位数及在院患者人数(人)",
                category: "综合ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "总数"
            },
            {
                id: 13,
                indicator: "床位数及在院患者人数(人)",
                category: "综合ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "新冠重症"
            },
            {
                id: 14,
                indicator: "床位数及在院患者人数(人)",
                category: "综合ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "新冠危重症"
            },
            {
                id: 15,
                indicator: "床位数及在院患者人数(人)",
                category: "综合ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "基础病重症"
            },
            {
                id: 16,
                indicator: "床位数及在院患者人数(人)",
                category: "呼吸专科ICU",
                sub_category: "患者",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "患者"
            },
            {
                id: 17,
                indicator: "床位数及在院患者人数(人)",
                category: "呼吸专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "新冠重症"
            },
            {
                id: 18,
                indicator: "床位数及在院患者人数(人)",
                category: "呼吸专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "新冠危重症"
            },
            {
                id: 19,
                indicator: "床位数及在院患者人数(人)",
                category: "呼吸专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "基础病重症"
            },
            {
                id: 20,
                indicator: "床位数及在院患者人数(人)",
                category: "呼吸专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "基础病重症"
            },
            {
                id: 21,
                indicator: "床位数及在院患者人数(人)",
                category: "其他专科ICU",
                sub_category: "患者",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "患者"
            },
            {
                id: 22,
                indicator: "床位数及在院患者人数(人)",
                category: "其他专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "新冠重症"
            },
            {
                id: 23,
                indicator: "床位数及在院患者人数(人)",
                category: "其他专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "新冠危重症"
            },
            {
                id: 24,
                indicator: "床位数及在院患者人数(人)",
                category: "其他专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "基础病重症"
            },
            {
                id: 25,
                indicator: "床位数及在院患者人数(人)",
                category: "其他专科ICU",
                sub_category: "新冠病毒核酸或抗原阳性数",
                count: "",
                note: "新冠病毒核酸或抗原阳性数",
                detail: "基础病重症"
            }
          ]
        }
      ],
      formData: {
        basicInfo: {
          report_id: "13000100000000001",
          reporter: "",
          contact: "",
          title: "二级及以上医疗机构医疗救治工作有关情况日报表",
          reportDate: this.getCurrentDate()
        }
      }
    }
  },
  created() {
    this.loadSavedData()
  },
  methods: {
    getCurrentDate() {
      const today = new Date()
      const year = today.getFullYear()
      const month = String(today.getMonth() + 1).padStart(2, '0')
      const day = String(today.getDate()).padStart(2, '0')
      return `${year}-${month}-${day}`
    },
    handleEnter(event) {
      event.preventDefault()
      const inputs = document.querySelectorAll('input[type="number"], input[type="text"], input[type="date"]')
      const currentIndex = Array.from(inputs).indexOf(event.target)
      const nextInput = inputs[currentIndex + 1]
      if (nextInput) {
        nextInput.focus()
        nextInput.select()
      }
    },
    async saveData() {
      try {
        const response = await fetch('http://127.0.0.1:8000/api/covid-reports', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(this.formData)
        })

        if (!response.ok) {
          const error = await response.json()
          throw new Error(error.detail || '保存失败')
        }

        const data = await response.json()
        ElMessage.success('数据保存成功！')
        localStorage.setItem('covidFormData', JSON.stringify(this.formData))
      } catch (error) {
        console.error('保存失败:', error)
        ElMessage.error(`保存失败: ${error.message}`)
      }
    },
    async exportExcel() {
      try {
        const response = await fetch(
          `http://127.0.0.1:8000/api/covid-reports/export?start_date=${this.formData.basicInfo.reportDate}&end_date=${this.formData.basicInfo.reportDate}`,
          {
            method: 'GET',
            mode: 'cors',
            credentials: 'omit'
          }
        )

        if (!response.ok) {
          throw new Error(`导出失败: ${response.status}`)
        }

        const blob = await response.blob()
        const url = window.URL.createObjectURL(blob)
        const a = document.createElement('a')
        a.href = url
        a.download = `医疗救治工作情况_${this.formData.basicInfo.reportDate}.xlsx`
        document.body.appendChild(a)
        a.click()

        window.URL.revokeObjectURL(url)
        document.body.removeChild(a)
      } catch (error) {
        console.error('导出Excel失败:', error)
        ElMessage.error(`导出Excel失败: ${error.message}`)
      }
    },
    loadSavedData() {
      const savedData = localStorage.getItem('covidFormData')
      if (savedData) {
        const data = JSON.parse(savedData)
        this.formData = data
      }
    }
  }
}
</script>

<style scoped>
/* 保持原有的样式不变 */
</style> 